public class SurroundedRegions {
    /**
     * 给你一个 m x n 的矩阵 board ，
     * 由若干字符 'X' 和 'O' ，找到所有被 'X' 围绕的区域，并将这些区域里所有的 'O' 用 'X' 填充。
     * @param board
     */
    public void solve(char[][] board) {

        for (int i=0;i<board.length;i++){
            dfs(board,i,0);
            dfs(board,i,board[0].length-1);
        }
        for (int j=0;j<board[0].length;j++){
            dfs(board,0,j);
            dfs(board,board.length-1,j);
        }

        for (int i=0;i<board.length;i++){
            for (int j=0;j<board[0].length;j++){
                if (board[i][j]=='O')
                    board[i][j]='X';
                else if (board[i][j]=='A')
                    board[i][j]='O';
            }
        }

    }

    public void dfs(char[][] board,int x,int y){
        if (x<0||x>=board[0].length||y<0||y>=board.length||board[x][y]!='O'){
            return;
        }
        board[x][y] = 'A';
        dfs(board,x-1,y);
        dfs(board,x,y-1);
        dfs(board,x,y+1);
        dfs(board,x+1,y);

    }
}
